/**
 * Carnegie Mellon University
 * School of Computer Science
 * MSIT eBusiness Technology
 * 
 * Team 8 - Infinity (2011 Spring)
 * infinity-team8@googlegroups.com
 * 
 * Professor: Jeffrey L. Eppinger
 * Program Faculty: Sujata Telang
 */

package model;

import java.util.Arrays;

import org.mybeans.dao.DAOException;
import org.mybeans.factory.BeanFactory;
import org.mybeans.factory.BeanFactoryException;
import org.mybeans.factory.BeanTable;
import org.mybeans.factory.DuplicateKeyException;
import org.mybeans.factory.RollbackException;
import org.mybeans.factory.Transaction;

import databean.GlobalBean;


public class GlobalDAO {
	private BeanFactory<GlobalBean> factory;
	
	public GlobalDAO() throws DAOException {
		try {
			// Get a BeanTable so we can create the "user" table
	        BeanTable<GlobalBean> globalTable = BeanTable.getInstance(GlobalBean.class,"global");
	        
	        if (!globalTable.exists()) globalTable.create("field");
	        
	        // Long running web apps need to clean up idle database connections.
	        // So we can tell each BeanTable to clean them up.  (You would only notice
	        // a problem after leaving your web app running for several hours.)
	        globalTable.setIdleConnectionCleanup(true);
	
	        // Get a BeanFactory which the actions will use to read and write rows of the "user" table
	        factory = globalTable.getFactory();
	        	        
		} catch (BeanFactoryException e) {
			throw new DAOException(e);
		}
	}
	
	public void create(GlobalBean global) throws DAOException {
		try {
	    	
        	Transaction.begin();
        	
        	GlobalBean dbGlobal = factory.create(global.getField());
        	dbGlobal.setDate(global.getDate());
        	
			Transaction.commit();
			
		} catch (DuplicateKeyException e) {
			throw new DAOException(e);
		} catch (RollbackException e) {
			throw new DAOException(e);
		} finally {
			if (Transaction.isActive()) Transaction.rollback();
		}
	}
	
	
	public GlobalBean lookup(String field) throws DAOException {
		try {
			
			if (factory.lookup(field) == null) {
				return null;
			} else {
				return factory.lookup(field);
			}
			
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}
	
	
	public void modify(GlobalBean global) throws DAOException {
		try {
    	
        	Transaction.begin();
        	
        	GlobalBean dbGlobal = factory.lookup(global.getField());
        	dbGlobal.setDate(global.getDate());
        	
			Transaction.commit();
			
		} catch (DuplicateKeyException e) {
			throw new DAOException(e);
		} catch (RollbackException e) {
			throw new DAOException(e);
		} finally {
			if (Transaction.isActive()) Transaction.rollback();
		}
	}
	
	
	protected BeanFactory<GlobalBean> getFactory() { return factory; }
	
	public GlobalBean[] getGlobals() throws DAOException {
		try {
			GlobalBean[] globals = factory.match();
			Arrays.sort(globals);
			return globals;
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}
	
}
